package com.android.lovely.ui.fragment.image

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.lovely.data.AiCreateUiEvent
import com.android.lovely.data.GeneratedModel
import com.android.lovely.databinding.ItemImageModelBinding
import com.android.lovely.databinding.ViewModelStyleBinding
import com.android.lovely.databinding.ViewRoleTitleBinding
import com.android.lovely.ext.loadWithPlaceholder
import com.android.lovely.framework.BaseAdapter
import com.kotlin.extensions.dimen.Dimension.dp
import com.kotlin.extensions.inflater.LayoutInflaterExt.layoutInflater
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/4/10 15:59
 */
class ModelStyleView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
    private val mBinding by lazy {
        ViewModelStyleBinding.inflate(layoutInflater, this, false)
    }

    val modeAdapter by lazy {
        ModelAdapter()
    }

    val styleAdapter by lazy {
        StyleAdapter()
    }

    var callBack: OnModelStyleChang? = null

    init {
        addView(mBinding.root)
        mBinding.rvModel.adapter = modeAdapter
        mBinding.rvModel.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        mBinding.rvStyle.adapter = styleAdapter
        mBinding.rvStyle.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)

        modeAdapter.onItemClick = { item, pos ->
            this.callBack?.onModeChange(item,pos)
        }

        styleAdapter.onItemClick = { item, pos ->
            this.callBack?.onStyleChange(item,pos)
        }
    }

    fun setListener(callBack: OnModelStyleChang) {
        this.callBack = callBack
    }

    fun update(models: List<GeneratedModel>, generatedModels: List<GeneratedModel>) {
        modeAdapter.updateList(models)
        styleAdapter.updateList(generatedModels)
    }

    fun getSelectModel(): GeneratedModel {
        return modeAdapter.getItems().firstOrNull { it.isSelected } ?: GeneratedModel()
    }

    fun getSelectStyle(): GeneratedModel {
        return styleAdapter.getItems().firstOrNull { it.isSelected } ?: GeneratedModel()
    }

    interface OnModelStyleChang {
        fun onModeChange(data: GeneratedModel?, position: Int)
        fun onStyleChange(data: GeneratedModel?, position: Int)
    }
}

class ModelAdapter : BaseAdapter<GeneratedModel, ItemImageModelBinding>() {
    override fun bindViewWithHolderDetails(
        holder: ViewHolder,
        item: GeneratedModel?,
        binding: ItemImageModelBinding,
        position: Int
    ) {
        item ?: return
        binding.apply {
            image.loadWithPlaceholder(item.thumbnail)
            tvReal.text = item.label
            tvReal.isSelected = item.isSelected
            root.clickable {
                if(item.isSelected){
                    return@clickable
                }
                onItemClick?.invoke(item, position)
            }
        }
    }
}

class StyleAdapter : BaseAdapter<GeneratedModel, ItemImageModelBinding>() {
    override fun bindViewWithHolderDetails(
        holder: ViewHolder,
        item: GeneratedModel?,
        binding: ItemImageModelBinding,
        position: Int
    ) {
        item ?: return
        binding.apply {
            image.loadWithPlaceholder(item.thumbnail)
            tvReal.text = item.label
            tvReal.isSelected = item.isSelected
            root.clickable {
                if(item.isSelected){
                    return@clickable
                }
                onItemClick?.invoke(item, position)
            }
        }
    }
}